package com.yanxu.lock.lock;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025/3/5-2025
 * @Description: 使用 try-lock 来避免死锁
 */
public class Lock_TryLockDeadlock implements Runnable {
    int flag = 1;
    private static Lock lock1 = new ReentrantLock();
    private static Lock lock2 = new ReentrantLock();

    public static void main(String[] args) {
        Lock_TryLockDeadlock r1 = new Lock_TryLockDeadlock();
        r1.flag = 1;
        new Thread(r1, "线程1").start();


        Lock_TryLockDeadlock r2 = new Lock_TryLockDeadlock();
        r2.flag = 2;
        new Thread(r2, "线程2").start();


    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (flag == 1) {
                try {
                    if (lock1.tryLock(800, TimeUnit.MILLISECONDS)) {
                        try {
                            System.out.println("线程1 获取到了锁1！");
                            Thread.sleep(new Random().nextInt(1000));
                            if (lock2.tryLock(800,TimeUnit.MICROSECONDS)){
                                try {
                                    System.out.println("线程1 获取到了锁2！");
                                    System.out.println("线程1 已经成功的获取到了2把锁!!!!!！执行自己的业务逻辑!!!");
                                    break;
                                }finally {
                                    lock2.unlock();
                                    Thread.sleep(new Random().nextInt(1000));
                                }
                            }else {
                                System.out.println("线程1 获取锁2失败, 重试！");
                            }
                        } finally {
                            lock1.unlock();
                            Thread.sleep(new Random().nextInt(1000));
                        }
                    } else {
                        System.out.println("线程1 获取锁1失败，重试！");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            if (flag == 2) {
                try {
                    if (lock2.tryLock(3000, TimeUnit.MILLISECONDS)) {
                        try {
                            System.out.println("线程2 获取到了锁2！");
                            Thread.sleep(new Random().nextInt(1000));
                            if (lock1.tryLock(800,TimeUnit.MICROSECONDS)){
                                try {
                                    System.out.println("线程2 获取到了锁1！");
                                    System.out.println("线程2 已经成功的获取到了2把锁!!!!!！执行自己的业务逻辑!!!");
                                    break;
                                }finally {
                                    lock1.unlock();
                                    Thread.sleep(new Random().nextInt(1000));
                                }
                            }else {
                                System.out.println("线程2 获取锁1失败, 重试！");
                            }
                        } finally {
                            lock2.unlock();
                            Thread.sleep(new Random().nextInt(1000));
                        }
                    } else {
                        System.out.println("线程2 获取锁2失败，重试！");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}